package com.hyfrogx.modules.information.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyfrogx.common.exception.ServiceException;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.modules.app.utils.StringUtils;
import com.hyfrogx.modules.information.entity.InfoClassify;
import com.hyfrogx.modules.information.form.InfoClassifyForm;
import com.hyfrogx.modules.information.service.InformationClassifyService;
import com.hyfrogx.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liubin
 * @date 2019/11/20 13:43
 */

@RestController
@RequestMapping(value = "information/classify")
@Api(value = "HyInformationClassifyController", tags = {"后台--资讯分类管理"})
public class HyInformationClassifyController extends AbstractController {
  @Autowired
  private InformationClassifyService infoClassifyService;

  /*
   * 获取所有分类
   *
   * @return
   */
  @RequestMapping(value = "getAllClassify", method = RequestMethod.GET)
  @ApiOperation("获取所有分类")
  public R getAllClassify() {
    try {
      List<InfoClassify> result = infoClassifyService.getBaseMapper()
        .selectList(new QueryWrapper<InfoClassify>().orderByAsc("sort"));
      return R.ok().put("result",result);
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  /*
   * 新增资讯分类
   *
   * @return
   */
  @PostMapping("addInfoClassify")
  @ApiOperation("新增资讯分类")
  public R addInfoClassify(@RequestBody InfoClassifyForm form) {
    try {
      InfoClassify infoClassify = new InfoClassify();
      BeanUtils.copyProperties(form,infoClassify);
      infoClassify.setCreator(getUserId().toString());
      infoClassifyService.addInfoClassify(infoClassify);
      return R.ok("操作成功");
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  /*
   * 资讯分类详情
   *
   * @return
   */
  @RequestMapping(value = "getInfoClassifyByid", method = RequestMethod.GET)
  @ApiOperation("资讯分类详情")
  public R getInfoClassifyByid(Integer id) {
    try {
      InfoClassify result = infoClassifyService.getBaseMapper().selectById(id);
      if(ObjectUtil.isNotNull(result)){
        return R.ok().put("result",result);

      }
      return R.error("未查询到此分类");

    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  /*
   * 编辑资讯分类
   *
   * @return
   */
  @RequestMapping(value = "updateInfoClassify", method = RequestMethod.POST)
  @ApiOperation("编辑资讯分类")
  public R updateInfoClassify(@RequestBody InfoClassify infoClassify) {
    try {
      if(infoClassify.getId()==null){
        return R.error("请选择分类");
      }
      infoClassifyService.updateById(infoClassify);
      return R.ok("操作成功");
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  /*
   * 删除资讯分类
   *
   * @return
   */
  @RequestMapping(value = "deleteInfoClassify", method = RequestMethod.DELETE)
  @ApiOperation("删除资讯分类")
  public R deleteInfoClassify(Integer id) {
    try {
      infoClassifyService.removeById(id);
      return R.ok("操作成功");
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  /*
   * 禁用资讯分类
   *
   * @return
   */
  @RequestMapping(value = "forbiddenInfoClassify", method = RequestMethod.GET)
  @ApiOperation("禁用资讯分类")
  public R forbiddenInfoClassify(Integer id) {
    try {
      InfoClassify infoClassify = new InfoClassify();
      infoClassify.setId(id);
      infoClassify.setClassifyState(0);
      infoClassify.setUpdateTime(new Date());
      infoClassifyService.updateById(infoClassify);
      return R.ok("操作成功");
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  /*
   * 开启资讯分类
   *
   * @return
   */
  @RequestMapping(value = "openInfoClassify", method = RequestMethod.GET)
  @ApiOperation("开启资讯分类")
  public R openInfoClassify(Integer id) {
    try {
      InfoClassify infoClassify = new InfoClassify();
      infoClassify.setId(id);
      infoClassify.setClassifyState(1);
      infoClassify.setUpdateTime(new Date());
      infoClassifyService.updateById(infoClassify);
      return R.ok("操作成功");
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  /**
   * 更新类别排序
   *
   * @return
   */
  @RequestMapping(value = "sort", method = RequestMethod.POST)
  @ApiOperation("更新类别排序")
  public R sort(String info) {
    if (StringUtils.isEmpty(info)) {
      return R.ok();
    }
    JSONArray ja = JSONArray.parseArray(info);
    List<InfoClassify> list = new ArrayList<InfoClassify>();
    for (int i = 0; i < ja.size(); i++) {
      InfoClassify object = ja.getObject(i, InfoClassify.class);
      list.add(object);
    }
    try {
      infoClassifyService.updateSort(list);
      return R.ok();
    } catch (ServiceException e) {
      return R.error(e.getMessage());
    }
  }
}
